// pages/search/search.js
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    id: '',
    historyList: [],
    from: ''
  },
  // 发生改变后文本框内容同步
  onChange(e) {
    this.setData({
      value: e.detail
    });
  },
  // 
  onSearch() {

  },
  // 点击跳转到列表页面
  onClick() {
    var that = this
    wx.navigateTo({
      url: "../product/product?name=" + that.data.value
    })
  },
  // 删除历史记录
  onDelete() {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + '/product/clearAllSearchHistory',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        xcxOpenId: app.globalData.xcxOpenId
      },
      success(res) {
        if (res.data.code == 0) {
          that.getoldValue()
        }
      }
    })
  },
  // 点击按钮自动跳转到相应的列表页面
  history(e) {
    var that = this
    wx.navigateTo({
      url: "../product/product?name=" + e.currentTarget.dataset.history
    })
  },
  getoldValue() {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + '/product/findAllSearchHistory',
      method: 'get',
      data: {
        xcxOpenId: app.globalData.xcxOpenId
      },
      success(res) {
        that.setData({
          historyList: res.data.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      from: options.from
    })
    var that = this
    if (that.data.value !== null) {
      that.getoldValue()
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (!this.data.from) {
      wx.reLaunch({
        url: '../index/index',
      })
    }else {
      wx.reLaunch({
        url: '../'+ this.data.from +'/'+ this.data.from,
      })
    }
  }
})